//<p>给定一个 <code>m x n</code> 二维字符网格 <code>board</code> 和一个字符串单词 <code>word</code> 。如果 <code>word</code> 存在于网格中，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>
//
//<p>单词必须按照字母顺序，通过相邻的单元格内的字母构成，其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。</p>
//
//<p> </p>
//
//<p><strong>示例 1：</strong></p>
//<img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/word2.jpg" style="width: 322px; height: 242px;" />
//<pre>
//<strong>输入：</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
//<strong>输出：</strong>true
//</pre>
//
//<p><strong>示例 2：</strong></p>
//<img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg" style="width: 322px; height: 242px;" />
//<pre>
//<strong>输入：</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
//<strong>输出：</strong>true
//</pre>
//
//<p><strong>示例 3：</strong></p>
//<img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/word3.jpg" style="width: 322px; height: 242px;" />
//<pre>
//<strong>输入：</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
//<strong>输出：</strong>false
//</pre>
//
//<p> </p>
//
//<p><strong>提示：</strong></p>
//
//<ul>
//	<li><code>m == board.length</code></li>
//	<li><code>n = board[i].length</code></li>
//	<li><code>1 <= m, n <= 6</code></li>
//	<li><code>1 <= word.length <= 15</code></li>
//	<li><code>board</code> 和 <code>word</code> 仅由大小写英文字母组成</li>
//</ul>
//
//<p> </p>
//
//<p><strong>进阶：</strong>你可以使用搜索剪枝的技术来优化解决方案，使其在 <code>board</code> 更大的情况下可以更快解决问题？</p>
//<div><div>Related Topics</div><div><li>数组</li><li>回溯</li><li>矩阵</li></div></div><br><div><li>👍 1316</li><li>👎 0</li></div>

package com.rising.leetcode.editor.cn;

import java.util.HashMap;
import java.util.Map;

/**
 * 单词搜索
 * @author DY Rising
 * @date 2022-05-26 19:36:00
 */
public class P79_WordSearch{
    public static void main(String[] args) {
        //测试代码
        Solution solution = new P79_WordSearch().new Solution();
        //输入：board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED" 输出：true
        System.out.println(solution.exist(new char[][]{{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}}, "ABCCED"));
    }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public boolean exist(char[][] board, String word) {
        char[] words = word.toCharArray();

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == words[0]) {
                    if (dfs(i, j, board, words, 0, new HashMap<>())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public boolean dfs(int row, int cul, char[][] board, char[] words, int index, Map<String, Integer> trace) {
        if (trace.get(row + "" + cul) != null || board[row][cul] != words[index]) return false;
        if (index == words.length - 1) return true;
        trace.put(row + "" + cul, 1);
        boolean rtn = false;
        //往上
        if (row - 1 >=0 && index < words.length - 1) {
            rtn = rtn || dfs(row - 1, cul, board, words, index + 1, trace);
        }
        //往右
        if (cul + 1 < board[0].length && index < words.length - 1) {
            rtn = rtn || dfs(row, cul + 1, board, words, index + 1, trace);
        }
        //往下
        if (row + 1 < board.length && index < words.length - 1) {
            rtn = rtn || dfs(row + 1, cul, board, words, index + 1, trace);
        }
        //往左
        if (cul - 1 >= 0 && index < words.length - 1) {
            rtn = rtn || dfs(row, cul - 1, board, words, index + 1, trace);
        }
        trace.remove(row + "" + cul);
        return rtn;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
